package question0;

public class SynchronizedMemberVariable implements Runnable {

    // 这是购票的总票数
    private int trainCount = 5;
    private final int finalTrainCount = trainCount;

    private final Object lock = new Object();


    @Override
    public void run() {
        synchronized (lock) {
//            System.out.println(Thread.currentThread().getName() + "拿到锁");
            if (trainCount > 0) {
                System.out.println(Thread.currentThread().getName() + ",出售 第" + (finalTrainCount - trainCount + 1) + "张票.");
                trainCount--;
            }
        }

    }
}
